package com.gimi.cloud.jimi.app.consumer.service.product.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gimi.cloud.jimi.common.utils.*;
import com.gimi.cloud.jimi.dto.product.ProductDto;

import org.apache.commons.lang3.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import com.gimi.cloud.jimi.app.consumer.feign.product.ProductFeignService;
import com.gimi.cloud.jimi.app.consumer.service.product.ProductService;
import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;

@Service
public class ProductServiceImpl implements ProductService {
    private static Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class);
    private final ProductFeignService productFeignService;

    @Autowired
    public ProductServiceImpl(ProductFeignService productFeignService) {
        this.productFeignService = productFeignService;
    }

    @Override
    public void showProductDetail(@RequestBody String requestData, HttpServletResponse response, HttpServletRequest request) throws MsgException {
        logger.info("srcData:" + requestData);
        ProductDto params = JsonUtil.jsonToBean(requestData, ProductDto.class);
        if (StringUtils.isNotEmpty(params.getProdId())) {
            Util.sendResponse(productFeignService.getProductDetail(params), response);
        } else {
            logger.error("请求参数错误");
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"商品id(prodId)不能为空");
        }
    }

	@Override
	public void getProductProp(@RequestBody String requestData, HttpServletResponse response, HttpServletRequest request)
			throws MsgException {
		logger.info("srcData:" + requestData);
		ProductDto params = JsonUtil.jsonToBean(requestData, ProductDto.class);
		if (StringUtils.isNotEmpty(params.getProdId())) {
			Util.sendResponse(productFeignService.getProductProp(params), response);
		} else {
			logger.error("请求参数错误");
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
	}

	@Override
	public void getModelValueList(String requestData, HttpServletResponse response, HttpServletRequest request)
			throws MsgException {
		logger.info("srcData:" + requestData);
		ProductDto params = JsonUtil.jsonToBean(requestData, ProductDto.class);
		if (StringUtils.isNotEmpty(params.getProdId())) {
			Util.sendResponse(productFeignService.getModelValueList(params), response);
		} else {
			logger.error("请求参数错误");
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
		
	}

   
}
